import { dateTimeFormat } from "@/utils/format"
import { getToken } from "@/utils/auth";
import { getAction, postActionByQueryString, postActionByJson } from "@/api/manage"

export const CommonMixins = {
  data() {
    return {
      /** 分页 **/
      /* 分页参数 前端分页 */
      iPagination:{
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      /* 分页参数 后端分页 */
      pagination: {
        showQuickJumper: true,
        showSizeChanger: true,
        pageSizeOptions: ['10', '20', '50'],
        current: 1,
        pageSize: 10,
        total: 0,
        onChange: (item) => {
          this.pagination.current = item
          this.handleSearch()
        },
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
        },
        onShowSizeChange: (current, pageSize) => {
          this.pagination.pageSize = pageSize
          this.handleSearch()
        },
      },
      /* 排序参数 */
      isorter:{
        column: 'createTime',
        order: 'desc',
      },
      /* 筛选参数 */
      filters: {},
      /* table加载状态 */
      loading:false,
      /* table选中keys*/
      selectedRowKeys: [],
      /* table选中records*/
      selectionRows: [],
      /* table 数据源 */
      dataSource: [],
      /* 搜索区域表单 */
      searchForm: {},
      /* 模态框 */
      dialog: {
        form: {},
        title: '',
        visible: false
      },
      /* 请求url */
      url: {
        findUserAllIdToName: 'system/user/findAllIdToName',
        getProjectMap: 'project/project/getProjectMap',
        getFile: 'system/file/getFile',
        getFileByTableId: 'system/file/getFileByTableId',
        listProjects: 'project/project/list'
      },
      /* 用户ID-Name映射 */
      userMap: {},
      /* 所有项目 */
      projects: [],
      /* 项目ID-Name映射 */
      projectMap: {},
    }
  },
  methods: {
    loadProjects() {
      return new Promise((resolve)=>{
        getAction(this.url.listProjects).then(resp => {
          resolve(resp.data)
        })
      })
    },
    /**
     * 递归删除树形结构数据为空的子节点
     * @param privileges
     * @returns {*}
     */
    deleteChildren(privileges) {
      if (privileges.length > 0){
        let arr = []
        privileges.map(item => {
          if(item.children && item.children.length == 0) {
            this.$delete(item, 'children')
          }else {
            item.children = this.deleteChildren(item.children)
          }
          arr.push(item)
        })
        return arr
      }else {
        return
      }
    },
    /**
     * 根据tableId查询文件并回显
     * @param params
     * @returns {Promise<any>}
     */
    loadFileByTableId(params) {
      return new Promise((resolve)=>{
        this.getAction(this.url.getFileByTableId, params).then(resp=>{
          let fileList = []
          resp.data.forEach(item => {
            let resp = item
            resp.error = 0
            resp.fileId = item.id
            fileList.push({
              uid: item.id,
              name: item.fileName,
              status: 'done',
              response: resp,
              url: this.getFile(item.url, 1),
            })
          })
          resolve(fileList)
        })
      })
    },
    /**
     * 下载/查看文件
     * @param url
     * @param openType
     * @returns {string}
     */
    getFile(url, openType) {
      return `${process.env.VUE_APP_BASE_API}${this.url.getFile}?url=${encodeURIComponent(encodeURIComponent(url))}&token=${getToken()}&openType=${openType}`
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      console.info(this.selectedRowKeys.length)
      //this.selectionRows = selectionRows;
    },
    async handleSearch() {
      this.loading = true
      this.selectedRowKeys = []
      this.dataSource = await this.loadData(this.searchForm)
    },
    loadData(params) {
      return new Promise((resolve)=>{
        getAction(this.url.list, params).then(resp=>{
          resolve(resp.data)
          this.loading = false
        }).catch(()=>{
          this.loading = false
        })
      })
    },
    loadUserMap() {
      return new Promise((resolve)=>{
        getAction(this.url.findUserAllIdToName).then(resp=>{
          resolve(resp.data)
        })
      })
    },
    loadProjectMap() {
      return new Promise((resolve)=>{
        getAction(this.url.getProjectMap).then(resp=>{
          resolve(resp.data)
        })
      })
    },
    handleTableChange(pagination, filters, sorter, callback) {
      console.log(sorter)
      let form = {}
      //分页、排序、筛选变化时触发
      //TODO 筛选
      if (Object.keys(sorter).length > 0) {
        if (sorter.order) {
          this.isorter.column = sorter.columnKey;
          this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
          if (callback) {
            form = Object.assign(this.searchForm, this.isorter)
          }else {
            this.searchForm = Object.assign(this.searchForm, this.isorter)
          }
        }else {
          this.$delete(this.searchForm, 'column')
          this.$delete(this.searchForm, 'order')
          this.$delete(form, 'column')
          this.$delete(form, 'order')
        }
      }
      this.ipagination = pagination
      if (callback) {
        callback(form)
      }else {
        this.handleSearch(true)
      }
    },
    dateTimeFormat,
  }
}
